package smoothing;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class WindowVotingSmoother implements Smoother {
	private int windowSize;

	@Override
	public void initialize(List<String> args) {
		windowSize = Integer.parseInt(args.get(0));
	}

	@Override
	public void smooth(List<Integer> list, List<Double> values) {
		List<Integer> lAlt = new ArrayList<Integer>(list.size());
		List<Double> vAlt = new ArrayList<Double>(list.size());
		
		for(int i = 0; i < list.size(); i++) {
			int begin = Math.max(i - windowSize, 0);
			int end = Math.min(i + windowSize, list.size() - 1);
			
			int positive = 0;
			int negative = 0;
			for(int j = begin; j <= end; j++) {
				if(list.get(j) == 0) {
					negative ++;
				} else {
					positive ++;
				}
			}
			
			if(positive >= negative) {
				lAlt.add(1);
			} else {
				lAlt.add(0);
			}
			
			List<Double> vs = new ArrayList<Double>(end - begin + 1);
			for(int j = begin; j <= end; j++) {
				vs.add(values.get(j));
			}
			Collections.sort(vs);
			vAlt.add(vs.get(vs.size()/2));
		}
		
		for(int i = 0; i < list.size(); i++) {
			list.set(i, lAlt.get(i));
			values.set(i, vAlt.get(i));
		}
		
	}

}
